﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net

Partial Public Class Sm01Popup
    Inherits System.Web.UI.Page

    Public ScriptTxt As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("B10Popup")

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        Dim adapter As New StrategyMappingDSTableAdapters.StrategyMappingTableAdapter
        Dim resultcheck = ""
        Dim PlanYear As String = Request("PlanYear")

        If FundCenter.Value = "" Then
            Alert("กรุณาเลือก Fund Center")
            Exit Sub
        End If
        If Cmmts.Value = "" Then
            Alert("กรุณาเลือก Commitment Item")
            Exit Sub
        End If
        If FundedProgram.Value = "" Then
            Alert("กรุณาเลือก Funded Program ")
            Exit Sub
        End If
        If FundCenter.Value = "" Then
            Alert("กรุณาเลือก Fund Center")
            Exit Sub
        End If
        If Strategy.Text = "" Then
            Alert("กรุณากรอก Strategy ")
            Exit Sub
        End If

        If Request("mode") = 1 Then
            
            Dim checkRecord As Decimal = 0
            checkRecord = adapter.ScalarQuery(Request("PlanYear"), FundCenter.Value, Cmmts.Value, FundedProgram.Value)
            If checkRecord > 0 Then
                Alert("มีข้อมูลนี้อยู่แล้วกรุณาเลือกข้อมูลเก่ามาแก้ไข ")
                Exit Sub
            Else
                adapter.InsertQuery(FundCenter.Value, Cmmts.Value, PlanYear, FundedProgram.Value, Strategy.Value, Session("users"), Session("users"))
            End If

            logger.Info("User " & Session("users") & " insert StrategyMapping ปี " & PlanYear &
                        " FundCenter " & FundCenter.Value & " Cmmts " & Cmmts.Value &
                        " FundedProgram " & FundedProgram.Value & " Strategy " & Strategy.Value)

        ElseIf Request("mode") = 2 Then



            Dim checkRecord As Decimal = 0
            checkRecord = adapter.ScalarQueryUpdate(PlanYear, FundCenter.Value, Cmmts.Value, FundedProgram.Value, Request("PlanYear"), Request("FundCenter"), Request("Cmmts"), Request("FundedProgram"))
            If checkRecord > 0 Then
                Alert("มีข้อมูลนี้อยู่แล้วกรุณาเลือกข้อมูลเก่ามาแก้ไข ")
                Exit Sub
            Else
                'adapter.UpdateQuery(FundCenter.Value, Cmmts.Value, PlanYear, FundedProgram.Value, Strategy.Value, Session("users"), Request("Id"))


                If (FundedProgram.Value = "OPEX") Then
                    adapter.UpdateQuery(FundCenter.Value, Cmmts.Value, PlanYear, FundedProgram.Value, Strategy.Value, Session("users"), Request("Id"))
                Else

                    'Check
                    If (rblUpdateWith.SelectedValue = "0") Then 'โครงการ
                        adapter.UpdateQueryByProject(Strategy.Value, Session("users"), FundedProgram.Value, PlanYear)
                    ElseIf (rblUpdateWith.SelectedValue = "1") Then 'กิจกรรม
                        adapter.UpdateQueryByActivity(Strategy.Value, Session("users"), FundedProgram.Value, PlanYear)
                    End If

                End If


            End If

            logger.Info("User " & Session("users") & " update StrategyMapping ปี " & PlanYear &
                        " FundCenter " & FundCenter.Value & " Cmmts " & Cmmts.Value &
                        " FundedProgram " & FundedProgram.Value & " Strategy " & Strategy.Value)

        End If
            ScriptTxt = "<script>window.parent.TINY.box.hide();</script>"
    End Sub

    Sub Alert(ByVal texter As String)
        ScriptTxt = "<script>alert('" & texter & "')</script>"
    End Sub

    Protected Sub CancelBtn_Click(sender As Object, e As ImageClickEventArgs) Handles CancelBtn.Click
        ScriptTxt = "<script>window.parent.TINY.box.hide();</script>"
    End Sub

    Function CheckProjectCode(ByVal projcode As String) As String
        Dim result As String = ""
        Dim errors As Decimal = 0
        Dim errorText As String = " กรุณาตรวจสอบ "
        Dim PlanYear As String = Val(Val(Request("PlanYear"))).ToString
        If FundCenter.Value = "" Then
            If projcode(0).ToString <> "P" And projcode(0).ToString <> "D" Then
                errorText += " รหัสโครงการต้องขึ้นต้นด้วย P หรือ D เท่านั้น " & " "
                errors = errors + 1
            End If
            If projcode.Substring(1, 2) <> PlanYear.Substring(2) Then

                errorText += " รหัสโครงการส่วนปี ต้องตรงกับปีประเมินเท่านั้น" & " "
                errors = errors + 1


            End If
            If projcode.Length > 12 Then
                errorText += " รหัสโครงการไม่สามารถยาวเกิน 12 ตัวอักษรได้ " & " "
                errors = errors + 1
            End If
        Else
            If projcode(0).ToString <> "P" And projcode(0).ToString <> "D" Then
                errorText += " รหัสโครงการต้องขึ้นต้นด้วย P หรือ D เท่านั้น " & " "
                errors = errors + 1
            End If
            If projcode.Length <> 5 Then
                errorText += " รหัสโครงการต้องมีความยาวที่ 5 ตัวอักษรเท่านั้น " & " "
                errors = errors + 1
            End If
        End If
        If errors = 0 Then
            Return "OK"
        Else
            Return errorText
        End If

    End Function

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then

        

            If Request("mode") = 1 Then
                FundCenter.Value = ""
                Cmmts.Value = ""
                FundedProgram.Value = ""
                Strategy.Value = ""
            Else

                Dim adapter As New StrategyMappingDSTableAdapters.StrategyMappingTableAdapter
                Dim dt As New StrategyMappingDS.StrategyMappingDataTable
                Dim s As String = Request("Id")
                dt = adapter.GetDataById(Convert.ToInt32(s))


                'Convert.ToInt32(Request("Id"))
                For Each row As StrategyMappingDS.StrategyMappingRow In dt
                    If row.FundCenter.Length > 0 Then
                        FundCenter.Value = row.FundCenter
                    End If
                    If row.CommitmentItem.Length > 0 Then
                        Cmmts.Value = row.CommitmentItem
                    End If
                    If row.FundedProgram.Length > 0 Then
                        FundedProgram.Value = row.FundedProgram
                    End If
                    If row.Strategy.Length > 0 Then
                        Strategy.Value = row.Strategy
                    End If
                Next

            End If
        End If

    End Sub
    
    Protected Sub FundCenter_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenter.InitializeDataSource
        Dim adap As New StrategyMappingDSTableAdapters.FundCentersTableAdapter
        Dim dt As New StrategyMappingDS.FundCentersDataTable

        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and  PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        'dt.Rows.Add("", "โครงการกลาง", "1", "0")


        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

        If dt.Count = 1 Then
            FundCenter.Value = dt.Item(0).FundCenterCode
        End If

    End Sub

    Protected Sub Cmmts_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenter.InitializeDataSource
        Dim adap As New StrategyMappingDSTableAdapters.CmmtsTableAdapter
        Dim dt As New StrategyMappingDS.CmmtsDataTable


        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and  PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " ORDER BY CmmtCode ASC"
        adap.GetDataByFilter(dt, sql)

        'dt.Rows.Add("", "โครงการกลาง", "1", "0")


        dt.DefaultView.Sort = "CmmtCode"
        e.DataSource = dt

        If dt.Count = 1 Then
            Cmmts.Value = dt.Item(0).CmmtCode

        End If

    End Sub

    Protected Sub FundedProgramProjects_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenter.InitializeDataSource
        Dim adap As New StrategyMappingDSTableAdapters.FundedProgramProjectsTableAdapter
        Dim dt As New StrategyMappingDS.FundedProgramProjectsDataTable

        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and Version = '0' AND  PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " ORDER BY FundedProgram ASC"
        adap.GetDataByFilter(dt, sql)

        'dt.Rows.Add("", "โครงการกลาง", "1", "0")


        dt.DefaultView.Sort = "FundedProgram"
        e.DataSource = dt

        If dt.Count = 1 Then
            FundedProgram.Value = dt.Item(0).FundedProgram

        End If

    End Sub

    Protected Sub Strategy_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenter.InitializeDataSource
        Dim adap As New StrategyMappingDSTableAdapters.StrategyTableAdapter
        Dim dt As New StrategyMappingDS.StrategyDataTable

        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and   PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " ORDER BY StrategyCode ASC"
        adap.GetDataByFilter(dt, sql)

        'dt.Rows.Add("", "โครงการกลาง", "1", "0")


        dt.DefaultView.Sort = "StrategyCode"
        e.DataSource = dt

        If dt.Count = 1 Then
            Strategy.Value = dt.Item(0).StrategyCode
        End If

    End Sub

End Class